/*
 * @Author: your name
 * @Date: 2019-12-16 10:13:36
 * @LastEditTime: 2020-02-19 10:11:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-admin-template\src\router\index.js
 */
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'home',
      component: () => import('@/views/home'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  }
]

export const asyncRoutes = [
  {
    path: '/region',
    component: Layout,
    redirect: '/region/manage',
    meta: {
      roles: ['area-view'],
      title: '区域'
    },
    children: [{
      path: 'manage',
      name: 'region-manage',
      component: () => import('@/views/region/region'),
      meta: { title: '区域管理', icon: 'example' }
    }]
  },
  {
    path: '/printer-manage',
    component: Layout,
    redirect: '/printer-manage',
    alwaysShow: true,
    name: 'printer-manage',
    meta: {
      title: '打印机管理',
      roles: ['printer-manage', 'cloud-printer-manage', 'consumable-view'],
      icon: 'documentation'
    },
    children: [
      {
        path: 'printer',
        name: 'printer',
        component: () => import('@/views/printer-manage/printer'),
        meta: {
          roles: ['printer-manage'],
          title: '打印机档案',
          icon: 'form'
        }
      },
      {
        path: 'cloud-printer',
        name: 'cloud-printer',
        component: () => import('@/views/printer-manage/cloud-printer'),
        meta: {
          roles: ['cloud-printer-manage'],
          title: '云打印机管理',
          icon: 'form'
        }
      },
      {
        path: 'print-material-application',
        name: 'print-material-application',
        component: () => import('@/views/printer-manage/print-material-application'),
        meta: {
          roles: ['consumable-view'],
          title: '打印耗材申请管理',
          icon: 'form'
        }
      }
    ]
  },
  {
    path: '/malfunction-manage',
    component: Layout,
    redirect: '/malfunction-manage',
    alwaysShow: true,
    name: 'malfunction-manage',
    meta: {
      roles: ['malfunction-view', 'error-code-view'],
      title: '故障申报管理',
      icon: 'peoples'
    },
    children: [
      {
        path: 'malfunction',
        name: 'malfunction',
        component: () => import('@/views/malfunction-manage/malfunction'),
        meta: {
          roles: ['malfunction-view'],
          title: '故障申报',
          icon: 'form'
        }
      },
      {
        path: 'error-code',
        name: 'error-code',
        component: () => import('@/views/malfunction-manage/error-code'),
        meta: {
          roles: ['error-code-view'],
          title: '错误代码速查',
          icon: 'documentation'
        }
      }
    ]
  },
  {
    path: '/order-manage',
    name: '/order-manage',
    meta: {
      title: '订单管理',
      icon: 'excel',
      roles: ['cloud-demand-order-view', 'cloud-print-order-view']
    },
    component: Layout,
    children: [{
      path: 'demand-order',
      name: 'demand-order',
      component: () => import('@/views/order-manage/demand-order'),
      meta: { roles: ['cloud-demand-order-view'], title: '云需求订单', icon: 'international' }
    }, {
      path: 'print-order',
      name: 'print-order',
      component: () => import('@/views/order-manage/print-order'),
      meta: { roles: ['cloud-print-order-view'], title: '云打印订单', icon: 'international' }
    }, {
      path: 'print-details',
      name: 'print-details',
      component: () => import('@/views/order-manage/print-details'),
      meta: { roles: ['cloud-print-order-view'], title: '云打印订单明细', icon: 'list' },
      hidden: true
    }, {
      path: 'demand-details',
      name: 'demand-details',
      component: () => import('@/views/order-manage/demand-details'),
      meta: { roles: ['cloud-demand-order-view'], title: '云需求订单明细', icon: 'list' },
      hidden: true
    }]
  },
  {
    path: '/advertising-manage',
    component: Layout,
    children: [{
      path: 'advertising-manage',
      name: 'advertising-manage',
      component: () => import('@/views/advertising-manage'),
      meta: { roles: ['advertising-manage'], title: '广告管理', icon: 'tab' }
    }]
  },
  {
    path: '/customer-manage',
    component: Layout,
    children: [{
      path: 'customer-manage',
      name: 'customer-manage',
      component: () => import('@/views/customer-manage'),
      meta: { roles: ['customer-view'], title: '用户管理', icon: 'user' }
    }]
  },
  {
    path: '/coupon-manage',
    component: Layout,
    redirect: '/coupon-manage',
    alwaysShow: true,
    name: 'coupon-manage',
    meta: {
      title: '优惠券管理',
      icon: 'documentation'
    },
    children: [
      {
        path: 'coupon-template',
        name: 'coupon-template',
        component: () => import('@/views/coupon-manage/coupon-template'),
        meta: { title: '优惠券模板', roles: ['coupon-template-manage'], icon: 'form' }
      },
      {
        path: 'coupon',
        name: 'coupon',
        component: () => import('@/views/coupon-manage/coupon'),
        meta: { title: '优惠券', roles: ['coupon-manage'], icon: 'form' }
      },
      {
        path: 'coupon-activity',
        name: 'coupon-activity',
        component: () => import('@/views/coupon-manage/coupon-activity'),
        meta: { title: '优惠券活动', roles: ['coupon-activity-manage'], icon: 'form' }
      }
    ]
  },
  // {
  //   path: '/coupon-manage',
  //   component: Layout,
  //   children: [{
  //     path: 'coupon-manage',
  //     name: 'coupon-manage',
  //     component: () => import('@/views/coupon-manage'),
  //     meta: { roles: ['discount-coupon-manage'], title: '优惠券管理', icon: 'star' }
  //   }]
  // },
  {
    path: '/revenue-expenditure-control',
    component: Layout,
    redirect: '/revenue-expenditure-control',
    alwaysShow: true,
    name: '/revenue-expenditure-control',
    meta: {
      roles: ['money-manage'],
      title: '账目管理',
      icon: 'clipboard'
    },
    children: [
      {
        path: 'revenue-manage',
        name: 'revenue-manage',
        component: () => import('@/views/revenue-expenditure-control/revenue-manage'),
        meta: {
          title: '收入管理',
          icon: 'form'
        }
      },
      {
        path: 'expenditure-manage',
        name: 'expenditure-manage',
        component: () => import('@/views/revenue-expenditure-control/expenditure-manage'),
        meta: {
          title: '支出管理',
          icon: 'form'
        }
      },
      // {
      //   path: 'withdrawal',
      //   name: 'withdrawal',
      //   component: () => import('@/views/revenue-expenditure-control/withdrawal/withdrawal'),
      //   meta: {
      //     title: '用户提现申请',
      //     icon: 'form'
      //   }
      // },
      {
        path: 'remittance',
        name: 'remittance',
        component: () => import('@/views/revenue-expenditure-control/remittance/remittance'),
        meta: {
          title: '用户交易记录',
          icon: 'form'
        }
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting',
    alwaysShow: true,
    name: 'setting',
    meta: {
      title: '设置',
      icon: 'component'
    },
    children: [
      {
        path: 'account-manage',
        name: 'account-manage',
        component: () => import('@/views/setting/account-manage'),
        meta: { title: '账号管理', roles: ['authority-manage'], icon: 'form' }
      },
      {
        path: 'authority-manage',
        name: 'authority-manage',
        component: () => import('@/views/setting/authority-manage'),
        meta: { title: '权限管理', roles: ['account-manage'], icon: 'form' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
